package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.CauseActions;
import com.jiantong.lawEnforcing.service.CauseActionsService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 案由列表
 * Created by mz03 on 2017/8/25.
 */
@Slf4j
@OpenBean
public class CauseActionsApis {
    @RpcConsumer
    private CauseActionsService causeActionsService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.causeActions.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public CauseActions findById(Long id){
        Response<CauseActions> response = causeActionsService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find news read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param causeActions
     * @return
     */
    @OpenMethod(
            key = "create.causeActions",
            paramNames = {"causeActions"},
            httpMethods = RequestMethod.POST
    )
    public Long create(CauseActions causeActions){
        Response<Long> response = causeActionsService.create(causeActions);
        if(!response.isSuccess()){
            log.error("failed to create causeActions read by causeActions = {}, cause : {}", causeActions, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.causeActions",
            paramNames = {"causeActions"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(CauseActions causeActions){
        if(causeActions.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = causeActionsService.update(causeActions);
        if(!response.isSuccess()){
            log.error("failed to update causeActions");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update causeActions");
        }
        return response.getResult();
    }

    /**
     * 根据状态分页查询
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @OpenMethod(
            key = "find.paging.CauseActions",
            paramNames = {"pageNo","pageSize","status"},
            httpMethods = RequestMethod.GET
    )
    public Paging<CauseActions> pagingCasesBasic(Integer pageNo, Integer pageSize, Integer status){
        Response<Paging<CauseActions>> response = causeActionsService.pagingByStatus(pageNo,pageSize,status);
        if(!response.isSuccess()){
            log.error("failed to find paging CauseActions status {}, cause : {}", status, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

}
